package com.example.bookstore.controller;

import com.example.bookstore.common.Result;
import com.example.bookstore.dto.BookCategoryTreeDto;
import com.example.bookstore.entity.Category;
import com.example.bookstore.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    CategoryService categoryService;

    //查询所有1级分类以及该分类下的2级分类
    @GetMapping
    public List<BookCategoryTreeDto> selectAllTreeNodes(){
        List<BookCategoryTreeDto> bookCategoryTreeDtos = categoryService.selectAllTreeNodes();
        return bookCategoryTreeDtos;
    }
    @GetMapping("/{mt}/{st}")
    public String getCategortById(@PathVariable Integer mt,@PathVariable Integer st){
        String name = categoryService.findNameByMtAndSt(mt,st);
        return name;
    }
    //查询选择的二级分类
    @GetMapping("/tree")
    public List<BookCategoryTreeDto> queryTreeNodes(){
        List<BookCategoryTreeDto> bookCategoryTreeDtos = categoryService.selectCategoryChild(1);
        return bookCategoryTreeDtos;
    }
    @PutMapping("/big")
    public void addBigCartgory(){
        categoryService.addBigCategory();
    }

    @PutMapping("/small/{id}")
    public void addSmallCartgory(@PathVariable Integer id){
        categoryService.addSmallCategory(id);
    }

    @PostMapping
    public Result<String> updateCartgory(@RequestBody Category category){
        Result<String> stringResult = categoryService.updateCategory(category);
        return stringResult;
    }

    @DeleteMapping("/{id}")
    public void deleteCartgory(@PathVariable Integer id){
        categoryService.deleteCategory(id);
    }

    @GetMapping("/child/{id}")
    public List<BookCategoryTreeDto> selectCategoryChild(@PathVariable int id){
        categoryService.selectCategoryChild(id);
        return null;
    }
    @GetMapping("/getById")
    public List<Category> selectCategoryByParentId(@RequestParam Integer parentId){
        List<Category> categories = categoryService.selectCategoryByParentId(parentId);
        return categories;
    }
}
